import "babel-polyfill"; //兼容IE浏览器
import Vue from 'vue'
import App from './App.vue'
//引入elementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
//引入VueRouter
import VueRouter from 'vue-router'
//引用routers.js
import routes from './routes'
//引入VueResource
import VueResource from 'vue-resource'
//引入字符图标
import '../icon/iconfont.css'
//引入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(VueQuillEditor)

//创建一个router实例
const router = new VueRouter({
	routes
})

router.beforeEach((to, from, next) => {
  	if (to.path === '/') {
    	sessionStorage.removeItem('user');
  	}
  
  	let user = JSON.parse(sessionStorage.getItem('user'));
	if (!user && to.path !== '/') {
    	next({ path: '/' })
  	} else {
    	next()
  	}
})

new Vue({
  router,
  methods : {
  	//判断菜单是否展示
    fnShowMenu(data) {
    	var routes = this.$router.options.routes
    	//当前用户为审核人
    	if(data.IsApprover) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.name =="会议室" || item.name =="个人中心") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
    					item.children[j].hidden = false
					}
	        	}
			}
    	}
    	
    	//当前用户为管理员
    	if(data.IsAdmin) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.path =="/home") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
						if(item.children[j].path !='/review') {
        					item.children[j].hidden = false
            			}
					}
				}
			}
    	}
    	
    	//当前用户非管理员、非审核人
    	if (!data.IsAdmin && !data.IsApprover) {
    		for(var i = 0; i < routes.length; i++) {
    			var item = routes[i]
    			if(item.name =="会议室" || item.name =="个人中心") {
    				item.hidden = false
					for(var j = 0; j < item.children.length; j++) {
						if (item.children[j].name !="审核") {
							item.children[j].hidden = false
						}
					}
    			}
			}
    	}
    }
  },
  mounted() {
  	let user = JSON.parse(sessionStorage.getItem('user'))
	if (user) { this.fnShowMenu(user) }
  },
  render: h => h(App)
}).$mount('#app')
